#ifndef RTREE_H_INCLUDED
#define RTREE_H_INCLUDED

#include <QtCore/qglobal.h>

class RTreeNode;
class PrimitiveBase;
class ObjectStorage;

class MAP_OBJECTS_DLL_EXPORT RTree
{
    friend class RTreeNode;

public:
    RTree(ObjectStorage *st, int least_ = 100, int most_ = 500);
    ~RTree();    
    void query ( const QRectF &QRectF, std::vector<PrimitiveBase *> & );
    void insert( PrimitiveBase *figure );
    QRectF boundingRect();

    void write(FILE *fp);
    void load(FILE *fp);
    bool isPreloaded();

private:
    RTreeNode *findSubtree ( PrimitiveBase *figure );
    PrimitiveBase *primitive(int id);
    void split( RTreeNode *son );    
    uint root;
    const int least;
    const int most;
    void verify(RTreeNode *node);
    RTreeNode *fromHandle(uint handle);
    ObjectStorage *storage;

    std::vector<RTreeNode *> nodes;
    int curHandle;
    bool preloaded;
};

#endif
